<?php
/*
 module:		红娘管理
 create_time:	2022-03-08 11:13:01
 author:		
 contact:		
*/

namespace app\backend\service;

use app\api\model\SysSite as SysSiteModel;
use app\backend\model\ZhMatchmakerLevel;
use app\backend\model\Member;
use app\backend\model\ZhMatchmaker;
use app\backend\model\ZhMatchmakerLevelOrder as ZhMatchmakerLevelOrderModel;
use think\exception\ValidateException;
use think\facade\Db;
use xhadmin\CommonService;

class ZhMatchmakerLevelOrderService extends CommonService
{

    /*
      * @Description  列表数据
      */
    public static function indexList($where, $limit, $page)
    {
        try {
            $res = ZhMatchmakerLevelOrderModel::alias('o')
                ->leftjoin('zh_matchmaker m','o.m_id=m.matchmaker_id')
                ->leftjoin('zh_matchmaker_level l','l.id = o.l_id')
                //                ->leftjoin('member a','zh_authentication.u_id = a.uid')
                ->where($where)
//                ->field("o.id,o.m_id,o.a_id,o.price,o.status,o.order_sn,o.price,o.createtime,zh_authentication.real_name,zh_matchmaker.name,
//                 zh_matchmaker_charge.entry_name,zh_matchmaker_charge.comment,zh_matchmaker_charge.is_fixed_price")
                ->field("o.id, o.price,o.status,o.order_sn, o.createtime,o.updatetime,o.m_id,o.l_id,l.name l_name,m.name
                 ")
                ->order(['createtime' => 'desc'])
                ->paginate(['list_rows'=>$limit,'page'=>$page])
                ->toArray();

        } catch (\Exception $e) {
            abort(config('my.error_log_code'), $e->getMessage());
        }
        return ['rows' => $res['data'], 'total' => $res['total']];
    }

    /*
     * @Description  添加
     */
    public static function add($data){
        try{
            $data['status'] = !is_null($data['status']) ? $data['status'] : '0';
            $data['createtime'] = time();
            $res = ZhMatchmakerLevelOrderModel::create($data);
        }catch(ValidateException $e){
            throw new ValidateException ($e->getError());
        }catch(\Exception $e){
            abort(config('my.error_log_code'),$e->getMessage());
        }
        return $res->id;
    }

    function view()
    {
        $id= $this->request->get('id');
        if(!$id) $this->error('参数错误');
        $sql = 'select  o.id,o.m_id, o.price,o.status,o.order_sn, o.createtime,
                gc_zh_matchmaker.name  m_name,l.id l_id,l.name l_name
                from gc_zh_matchmaker_level_order  as o  
                left join gc_zh_matchmaker  on o.m_id=gc_zh_matchmaker.matchmaker_id
                left join gc_zh_matchmaker_level  l on  o.l_id=l.id
                where o.id = '.$id.' limit 1';
        $info = Db::connect('mysql')->query($sql)[0];

        $info["createtime"]=date("Y-m-d H:i:s",$info["createtime"]);

        return $this->ajaxReturn($this->successCode, '返回成功', $info);
    }




}

